//
//  XLTgLabelView.m
//  XLPersonCenter
//
//  Created by 宋振 on 2018/7/23.
//  Copyright © 2018年 夜幕繁华. All rights reserved.
//

#import "XLTgLabelView.h"

@implementation XLTgLabelView
{
    //存储界面高度
    CGFloat viewHeight;
}

- (instancetype)initWithFrame:(CGRect)frame andLabelArray:(NSMutableArray *)labelArray
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        //获取标签个数
        
        if (!self.labelArray) {
            self.labelArray = [NSMutableArray arrayWithCapacity:1];
        }
        
        self.labelArray = labelArray;
        
        /**
         设置主要UI
         */
        [self setCustomeView];
        
    }
    
    return self;
}

#pragma mark    - - - 设置主要界面显示 - - -

/**
 设置主要UI
 */
- (void)setCustomeView
{
    //创建标签按钮
    [self layOutLabels];
}

/**
 创建标签按钮
 */
- (void)layOutLabels
{
    //按钮高度
    CGFloat labelHeight = 35;
    
    //距离左边的距离
    CGFloat labelLeftSpace = 16;
    
    //距离上面的距离
    CGFloat labelTopSpcace = 10;
    
    //按钮左右间隙
    CGFloat labelMarginX = 9;
    
    CGFloat labelMarginY = 10;
    
    //文字间隙
    CGFloat labelTextMargin = 10;
    
    //开始创建标签
    for (NSInteger i = 0; i < self.labelArray.count; i ++) {
        
        UIButton *tempButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:15];
        //设置frame
        tempButton.frame = CGRectMake(labelMarginX+labelLeftSpace, labelTopSpcace, 100, labelHeight);
        
        //设置字体
        [tempButton setTitle:self.labelArray[i] forState:(UIControlStateNormal)];
        
        [tempButton setTitleColor:[UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1] forState:(UIControlStateNormal)];
        
//        [tempButton setTintColor:[UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1]];
//        self.introduceLbl.font = [UIFont fontWithName:KFontNamePFMedium size:kScale(15)];
//        self.introduceLbl.textColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1];
        
        //设置标志
        tempButton.tag = 100 + i;
        
        //边框设置
        tempButton.layer.borderColor = RGB_HEX(0xa3a3a3).CGColor;
        tempButton.layer.borderWidth = 0.5f;
        
        //设置圆角
        tempButton.layer.cornerRadius = 5;
        tempButton.layer.masksToBounds = YES;
        
        //处理按钮的距离间隙
        [self setLabelMarginWithLabel:tempButton textFontMargin:labelTextMargin];
        
        //处理换行的问题
        //首先计算当前标签的总宽度
        CGFloat currentLabelW = tempButton.frame.origin.x + tempButton.frame.size.width + labelMarginX;
        
        //是否大于当前容器的宽度
        if (currentLabelW > self.frame.size.width) {
            
            //需要换行
            labelTopSpcace += labelHeight + labelMarginY;
            
            //刷新左边的距离 不在按照刚才的排列继续执行
            labelLeftSpace = 16;
            
            //刷新当前标签所在位置
            tempButton.frame = CGRectMake(labelMarginX + labelLeftSpace, labelTopSpcace, 100, labelHeight);
            
            //处理按钮的间隙
            [self setLabelMarginWithLabel:tempButton textFontMargin:labelTextMargin];
        }
        
        //重置高度
        CGRect rect = tempButton.frame;
        rect.size.height = labelHeight;
        tempButton.frame = rect;
        
        //添加事件
        [tempButton addTarget:self action:@selector(clickLabelAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //增加排序距离
        labelLeftSpace += tempButton.frame.size.width + labelMarginX;
        
        [self addSubview:tempButton];
        
        //记录高度
        if (i == self.labelArray.count - 1) {
            viewHeight = tempButton.frame.origin.y + tempButton.frame.size.height + 15;
            NSLog(@"%.2f",viewHeight);
        }
    }
    
    
    
    
}

/**
 设置标签的间隙距离
 */
- (void)setLabelMarginWithLabel:(UIButton *)tempButton textFontMargin:(CGFloat)margin
{
    //处理按钮的距离间隙
    [tempButton sizeToFit];
    
    //计算文字间隙
    CGRect rect = tempButton.frame;
    
    //左右两边的距离
    rect.size.width += margin*2;
    
    //重新设置frame
    tempButton.frame = rect;
}

#pragma mark    - - - 重置按钮事件 - - -

- (void)setLabelArray:(NSMutableArray *)labelArray
{
    
    if (_labelArray) {
        [_labelArray removeAllObjects];
    }
    
    //设置修饰标签
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
    
    for (NSInteger i = 0; i < labelArray.count; i ++) {
        NSString *name;
        NSString *times;
        if ([labelArray[i] isKindOfClass:[NSDictionary class]]){
            NSDictionary *dic = labelArray[i];
            name = dic[@"LabelName"];
            times = dic[@"LabelTimes"];
        }else{
            name = labelArray[i];
            times = @"";
        }
       
        
        //归纳标签显示
        NSString *labels = [NSString stringWithFormat:@"%@ (%@)",name,times];
        
        [tempArray addObject:labels];
        
    }
    
    //重置标签信息
    _labelArray = [tempArray mutableCopy];
    
    if (_labelArray.count > 0) {
        [self resetTagLabels];
    }
    
}

/**
 重置按钮显示
 */
- (void)resetTagLabels
{
    //移除所有标签 重新设置
    for (UIButton *btn in self.subviews) {
        [btn removeFromSuperview];
    }
    
    //重新加载标签
    [self layOutLabels];
}


#pragma mark    - - - 按钮事件 - - -
- (void)clickLabelAction:(UIButton *)sender
{
    NSLog(@"点击了标签按钮 %ld",sender.tag);
}


#pragma mark    - - - Other Method - - -

//获取最大高度 适应约束
- (CGFloat)getViewHeight
{
    if (viewHeight == 0) {
        viewHeight = 0.01;
    }
    
    return viewHeight;
}







#pragma mark     - - - 懒加载 - - -


#pragma mark     - - - Setter - - -












@end
